package com.centling.smartSealForPhone.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.centling.smartSealForPhone.R;

import java.util.List;

/**
 *
 * @author lenovo
 * @date 2016/4/15
 */
public class RvStampImgAdapter extends RecyclerView.Adapter<RvStampImgAdapter.MyViewHolder> {
    private Context context;
    private List<Bitmap> bitmaps;

    public RvStampImgAdapter(Context context,List<Bitmap> bitmaps) {
        this.context = context;
        this.bitmaps=bitmaps;

    }

    @Override
    public RvStampImgAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_stamp_img, parent, false);
        return new MyViewHolder(view);
    }

    @Override
    public void onBindViewHolder(final RvStampImgAdapter.MyViewHolder holder, final int position) {
        if (position==bitmaps.size()){
            holder.ivAdd.setVisibility(View.VISIBLE);
            holder.ivImg.setVisibility(View.INVISIBLE);
        }else {
            holder.ivAdd.setVisibility(View.INVISIBLE);
            holder.ivImg.setVisibility(View.VISIBLE);
            holder.ivImg.setImageBitmap(bitmaps.get(position));
        }

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (onItemClickListener != null) {
                    onItemClickListener.onItemClick(holder.itemView, position);
                }
            }
        });
    }

    @Override
    public int getItemViewType(int position) {
        return position;
    }

    @Override
    public int getItemCount() {
        return bitmaps.size()+1;
    }

    class MyViewHolder extends RecyclerView.ViewHolder {
        ImageView ivAdd,ivImg;

        MyViewHolder(View itemView) {
            super(itemView);
            ivAdd = itemView.findViewById(R.id.iv_item_add);
            ivImg = itemView.findViewById(R.id.iv_item_img);
        }
    }

    /**
     * list点击的接口
     */
    public interface OnItemClickListener {
        void onItemClick(View view, int postion);
    }

    private OnItemClickListener onItemClickListener;

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }
}
